package cn.JHai.rbac.controller;


import cn.JHai.rbac.RequiredPermission;
import cn.JHai.rbac.domain.Employee;
import cn.JHai.rbac.query.PageResult;
import cn.JHai.rbac.query.QueryObject;
import cn.JHai.rbac.service.DepartmentService;
import cn.JHai.rbac.service.EmployeeService;
import cn.JHai.rbac.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @Autowired
    RoleService roleService;

    @Autowired
    DepartmentService departmentService;
    /**
     * 显示employee，运用了分页
     * @param queryObject
     * @param model
     * @return
     */
    @RequestMapping("/list")
    @RequiredPermission({"获取员工","employee:list"})
    public String list(QueryObject queryObject , Model model){
        PageResult result = employeeService.query(queryObject);
        model.addAttribute("result",result);
        return "/employee/list";
    }

    /**
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @RequiredPermission({"删除员工","employee:delete"})
    public String list(  Model model,Long id){
        employeeService.Delete(id);
        return "redirect:/employee/list";
    }


    @RequestMapping("input")
    public String input(Model model,Long id){
        if(id!=null){

            model.addAttribute("entity",employeeService.get(id));
        }
        model.addAttribute("depts",departmentService.findAll());
        model.addAttribute("roles",roleService.findAll());
        return "employee/input";
    }

    @RequestMapping("saveOrUpdate")
    @RequiredPermission({"更新和添加员工","employee:saveOrUpdate"})
    public String saveOrUpdate(Employee employee, Long ids[]){
        employeeService.saveOrUpdata(employee,ids);
        return "redirect:/employee/list";
    }
}
